home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume4 / tm_to_time < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  3.7 KB

  1. From: "Oliver Laumann" <talcott!seismo!unido!tub!net>
  2. Subject: tm_to_time(3) -- convert broken-down time into time_t.
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 4, Issue 38
  7. Submitted by: "Oliver Laumann" <seismo!unido!tub!net>
  8.  
  9.  
  10. A number of requests have been posted to the net in the past asking
  11. for a function that can be used to convert a broken-down time (such
  12. as returned by localtime()) into a time_t (such as returned by
  13. time()).
  14. The following shell archive contains a function (plus manual page)
  15. that performs this conversion.
  16.  
  17. Regards,
  18.     Oliver Laumann, Technical University of Berlin, Germany.
  19.     ...ihnp4!seismo!unido!tub!net   or   net@DB0TUI6.BITNET
  20.           ...!mcvax!unido!tub!net
  21.  
  22. ---------------cut here---------------cut here---------------
  23. #! /bin/sh
  24. # This is a shell archive, meaning:
  25. # 1. Remove everything above the #! /bin/sh line.
  26. # 2. Save the resulting text in a file.
  27. # 3. Execute the file with /bin/sh (not csh) to create the files:
  28. #    tm_to_time.3
  29. #    tm_to_time.c
  30. # This archive created: Wed Mar 19 13:04:07 1986
  31. export PATH; PATH=/bin:$PATH
  32. echo shar: extracting "'tm_to_time.3'" '(683 characters)'
  33. if test -f 'tm_to_time.3'
  34. then
  35.     echo shar: will not over-write existing file "'tm_to_time.3'"
  36. else
  37. cat << \SHAR_EOF > 'tm_to_time.3'
  38. .TH MKTIME 3 "19 March 1986"
  39. .SH NAME
  40. tm_to_time \- convert broken-down time into time_t
  41. .SH SYNOPSIS
  42. .nf
  43. .B #include <sys/types.h>
  44. .B #include <sys/time.h>
  45. .PP
  46. .B time_t tm_to_time (tp);
  47. .B struct tm *tp;
  48. .fi
  49. .SH DESCRIPTION
  50. .I Tm_to_time
  51. converts a broken-down time pointed to by
  52. .I tp
  53. (such as returned by
  54. .IR localtime )
  55. into a
  56. .I time_t
  57. (that is, a number of seconds since 00:00:00 GMT, Jan 1, 1970).
  58. Thus, if
  59. .I tm_to_time
  60. is called with a broken-down time
  61. returned by a call to
  62. .I localtime
  63. with some
  64. .I time_t
  65. argument,
  66. the value obtained by
  67. .I tm_to_time
  68. is identical to the original
  69. .IR time_t .
  70. .SH AUTHOR
  71. Oliver Laumann
  72. .SH SEE ALSO
  73. gettimeofday(2) or time(2), ctime(3).
  74. SHAR_EOF
  75. if test 683 -ne "`wc -c < 'tm_to_time.3'`"
  76. then
  77.     echo shar: error transmitting "'tm_to_time.3'" '(should have been 683 characters)'
  78. fi
  79. fi # end of overwriting check
  80. echo shar: extracting "'tm_to_time.c'" '(1192 characters)'
  81. if test -f 'tm_to_time.c'
  82. then
  83.     echo shar: will not over-write existing file "'tm_to_time.c'"
  84. else
  85. cat << \SHAR_EOF > 'tm_to_time.c'
  86. #include <sys/types.h>
  87. #include <sys/time.h>
  88.  
  89. /* Return 1 if `y' is a leap year, 0 otherwise.
  90.  */
  91.  
  92. static int leap (y) int y; {
  93.     y += 1900;
  94.     if (y % 400 == 0)
  95.     return (1);
  96.     if (y % 100 == 0)
  97.     return (0);
  98.     return (y % 4 == 0);
  99. }
  100.  
  101. /* Return the number of days between Jan 1, 1970 and the given
  102.  * broken-down time.
  103.  */
  104.  
  105. static int ndays (p) struct tm *p; {
  106.     register n = p->tm_mday;
  107.     register m, y;
  108.     register char *md = "\37\34\37\36\37\36\37\37\36\37\36\37";
  109.  
  110.     for (y = 70; y < p->tm_year; ++y) {
  111.     n += 365;
  112.     if (leap (y)) ++n;
  113.     }
  114.     for (m = 0; m < p->tm_mon; ++m)
  115.     n += md[m] + (m == 1 && leap (y));
  116.     return (n);
  117. }
  118.  
  119. /* Convert a broken-down time (such as returned by localtime())
  120.  * back into a `time_t'.
  121.  */
  122.  
  123. time_t tm_to_time (tp) struct tm *tp; {
  124.     register int m1, m2;
  125.     time_t t;
  126.     struct tm otm;
  127.  
  128.     t = (ndays (tp) - 1) * 86400L + tp->tm_hour * 3600L
  129.     + tp->tm_min * 60 + tp->tm_sec;
  130.     /*
  131.      * Now the hard part -- correct for the time zone:
  132.      */
  133.     otm = *tp;
  134.     tp = localtime (&t);
  135.     m1 = tp->tm_hour * 60 + tp->tm_min;
  136.     m2 = otm.tm_hour * 60 + otm.tm_min;
  137.     t -= ((m1 - m2 + 720 + 1440) % 1440 - 720) * 60L;
  138.     return (t);
  139. }
  140. SHAR_EOF
  141. if test 1192 -ne "`wc -c < 'tm_to_time.c'`"
  142. then
  143.     echo shar: error transmitting "'tm_to_time.c'" '(should have been 1192 characters)'
  144. fi
  145. fi # end of overwriting check
  146. #    End of shell archive
  147. exit 0
  148.  
  149.